package com.heliang.ui;

import java.lang.reflect.Field;

import net.tsz.afinal.annotation.view.EventListener;
import net.tsz.afinal.annotation.view.Select;
import net.tsz.afinal.annotation.view.ViewInject;
import android.app.Activity;
import android.text.TextUtils;
import android.view.View;
import android.widget.AbsListView;

public class FinalActivityHelper {
    
    private Activity activity ;
    
    public FinalActivityHelper(Activity activity)
    {
        this.activity = activity; 
    }

    public void initView()
    {
        Field[] fields = activity.getClass().getDeclaredFields();
        if(fields == null ||  fields.length == 0)
            return ;
        
        for(Field field : fields)
        {
            ViewInject viewInject = field.getAnnotation(ViewInject.class);
            if(viewInject == null)
                continue ;
            
            int viewId = viewInject.id();
            View view = activity.findViewById(viewId);
            
            if ( view == null )
                continue ;
            
            try {
                field.setAccessible(true);
                field.set(activity,view );
            } catch (Exception e) {
                e.printStackTrace();
            }
            
            String clickMethod = viewInject.click();
            if(!TextUtils.isEmpty(clickMethod))
                setViewClickListener(view,clickMethod);
            
            String longClickMethod = viewInject.longClick();
            if(!TextUtils.isEmpty(longClickMethod))
                setViewLongClickListener(view,longClickMethod);
            
            String itemClickMethod = viewInject.itemClick();
            if(!TextUtils.isEmpty(itemClickMethod))
                setItemClickListener(view,itemClickMethod);
            
            String itemLongClickMethod = viewInject.itemLongClick();
            if(!TextUtils.isEmpty(itemLongClickMethod))
                setItemLongClickListener(view,itemLongClickMethod);
            
            Select select = viewInject.select();
            if(!TextUtils.isEmpty(select.selected()))
                setViewSelectListener(view,select.selected(),select.noSelected());
        }   
    }
    
    
    private void setViewClickListener(View view,String clickMethod)
    {
        view.setOnClickListener(new EventListener(activity).click(clickMethod));
    }
    
    private void setViewLongClickListener(View view,String clickMethod)
    {
        view.setOnLongClickListener(new EventListener(activity).longClick(clickMethod));
    }
    
    private void setItemClickListener(View view,String itemClickMethod)
    {
        if(view instanceof AbsListView)
        {
            ((AbsListView)view).setOnItemClickListener(new EventListener(activity).itemClick(itemClickMethod));
        }
    }
    
    private void setItemLongClickListener(View view,String itemClickMethod)
    {
        if(view instanceof AbsListView)
        {
            ((AbsListView)view).setOnItemLongClickListener(new EventListener(activity).itemLongClick(itemClickMethod));
        }
    }
    
    private void setViewSelectListener(View view,String select,String noSelect)
    {
        if(view instanceof AbsListView)
        {
            ((AbsListView)view).setOnItemSelectedListener(new EventListener(activity).select(select).noSelect(noSelect));
        }

    }
}
